package com.an.spring.beanlife;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 *
 * 实现各种生命周期控制访问的Car
 */

//管理BEan生命周期的接口
    //bean级的生命接口控制BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean
public class Car implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean
{
    private String brand;

    private String color;

    private int maxSpeed;

    private BeanFactory beanFactory;

    private String beanName;

    public void setColor(String color) {
        this.color = color;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public String getBrand() {
        return brand;
    }

    public String getColor() {
        return color;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public  Car(){
        System.out.println("调用无参的构造函数Car()");
    }

    public void setBrand(String brand){
        System.out.println("调用setBrand（）设置属性");
        this.brand = brand;
    }

    public String introduce() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                ", maxSpeed=" + maxSpeed +
                ", beanFactory=" + beanFactory +
                ", beanName='" + beanName + '\'' +
                '}';
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                ", maxSpeed=" + maxSpeed +
                ", beanFactory=" + beanFactory +
                ", beanName='" + beanName + '\'' +
                '}';
    }

    //BeanFactoryAware接口方法
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("调用BeanFactoryAware.setBeanFactory()。");
        this.beanFactory = beanFactory;
    }

    //BeanNameAware接口方法
    @Override
    public void setBeanName(String s) {

        System.out.println("调用BeanNameAware.setBeanName()");
        this.beanName = s;
    }

    //DisposableBean接口方法
    @Override
    public void destroy() throws Exception {

        System.out.println("调用DisposableBean.destroy()");
    }

    //InitializingBean接口方法
    @Override
    public void afterPropertiesSet() throws Exception {

        System.out.println("调用InitializingBean。afterPropertiesSet（）");
    }

    //通过Bean的init-method属性指定的初始化方法
    public void  myInit(){
        System.out.println("调用init-method属性指定的初始化方法，设置速度最大值为240");
        this.maxSpeed = 240;
    }

    //通过<bean>的destroy-method属性指定的销毁方法
    public void myDestroy(){
        System.out.println("调用destroy-method所指定的myDestroy（）。");
    }

}
